{% extends "bootstrap/base.html" %}

{% block title %}
{% if title %} {{ title }} - MicroBlog{% else %}{{ _("Welcome to") }} MicroBlog{% endif %}
{% endblock %}

{% block navbar %}
<nav class="navbar navbar-default">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" , data-target="#bs-example-navbar-collapse-1"
                aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="{{ url_for('main.index') }}">{{ _("MicroBlog") }}</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="{{ url_for('main.index') }}">{{ _("Home") }}</a></li>
                <li><a href="{{ url_for('main.explore') }}">{{ _("Explore") }}</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                {% if current_user.is_anonymous %}
                <li><a href="{{ url_for('auth.login') }}">{{ _("Login") }}</a></li>
                {% else %}
                <li><a href="{{ url_for('main.user', username=current_user.username) }}">{{ _("Profile") }}</a></li>
                <li><a href="{{ url_for('auth.logout') }}">{{ _("Logout") }}</a></li>
                {% endif %}
            </ul>
        </div>
    </div>
</nav>
{% endblock %}

{% block content %}
<div class="container">
    {% with messages = get_flashed_messages() %}
    {% if messages %}
    {% for message in messages %}
    <div class="alert alert-info" role="alert">{{ message }}</div>
    {% endfor %}
    {% endif %}
    {% endwith %}

    {# application content needs to be provided in the app_content block #}
    {% block app_content %}{% endblock %}
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{{ moment.lang(g.locale) }}
<script>
    function translate(sourceElem, destElem, sourceLang, destLang) {
        $(destElem).html("<img src={{ url_for('static', filename='loading.gif') }}>");
        $.post("/translate", {
            text: $(sourceElem).text(),
            source_language: sourceLang,
            dest_language: destLang
        }).done(function (response) {
            $(destElem).text(response["text"])
        }).fail(function () {
            $(destElem).text("{{ _('Error: Could not contact server.') }}");
        });
    }

    $(function () {
        var timer = null;
        var xhr = null;
        $(".user_popup").hover(
            function (event) {
                var elem = $(event.currentTarget);
                timer = setTimeout(function () {
                    timer = null;
                    xhr = $.ajax("/user/" + elem.first().text().trim() + "/popup").done(
                        function (data) {
                            xhr = null;
                            elem.popover({
                                trigger: "manual",
                                html: true,
                                animation: false,
                                container: elem,
                                content: data
                            }).popover("show");
                            flask_moment_render_all();
                        }
                    );
                }, 1000);
            },
            function (event) {
                var elem = $(event.currentTarget);
                if (timer) {
                    clearTimeout(timer);
                    timer = null;
                }
                else if (xhr) {
                    xhr.abort();
                    xhr = null;
                }
                else {
                    elem.popover("destroy");
                }
            }
        )
    });
</script>
{% endblock %}
